//
//  SuggestionsController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/9.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "SuggestionsController.h"
#import "Config.h"

static NSString * const string = @"    您好！感谢您来我院就诊，请把您就诊期间的真实感受告诉我们，以便我们及时改进。谢谢您的合作！";

@interface SuggestionsController () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation SuggestionsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"建议意见";
    self.mainView.layer.cornerRadius = 5.0f;
    self.mainView.layer.masksToBounds = YES;
    self.mainView.layer.borderWidth = 0.5f;
    self.mainView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 提交
- (IBAction)sendSuggestion:(UIButton *)sender {
    // ...
    NSString *text = [self.textView.text stringRemoveSpecialSymbol:@" "];
    if (text.length && ![text isEqualToString:[string stringRemoveSpecialSymbol:@" "]]) {
        NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
        NSDictionary *param = @{@"type" : @"1", @"content" : text};
        [HTTPSTool POSTRequestWithPath:XHUserAddSuggest cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//            XHLog(@"建议意见 -> JSON: %@", JSON);
            [self.textView resignFirstResponder];
            [ZYToast showBottomText:@"提交成功!"];
            self.textView.textColor = [UIColor lightGrayColor];
            self.textView.text = string;
        }];
    } else {
        [self setAlertViewWithMessage:@"请输入正确的建议意见!"];
    }
}

#pragma mark - textView delegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    textView.textColor = [UIColor blackColor];
    textView.text = nil;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    textView.text = [textView.text stringRemoveSpecialSymbol:@" "];
    if (!textView.text.length) {
        textView.textColor = [UIColor lightGrayColor];
        textView.text = string;
    }
}

#pragma mark - AlertView
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textView resignFirstResponder];
}

@end
